//
//  XNPageView.m
//  Project
//
//  Created by Z on 2019/6/28.
//  Copyright © 2019年 YANJING. All rights reserved.
//

#import "XNPageView.h"

@interface XNPageView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *dotView;//选中的
@property (nonatomic, strong) NSMutableArray *indexs;

@end
@implementation XNPageView

+ (instancetype)new
{
    return [[XNPageView alloc] init];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _numberOfPages = 0;
        _currentPage = 0;
        _gap = 4;
        _offset = 0;
        _normalColor = [UIColor colorWithWhite:0 alpha:.2];
        _selectColor = [UIColor colorWithWhite:1 alpha:1];
        _normalSize = CGSizeMake(4, 4);
        _selectSize = CGSizeMake(4, 4);
    }
    return self;
}

- (void)setNumberOfPages:(NSInteger)numberOfPages
{
    if(_numberOfPages==numberOfPages)return;
    _numberOfPages = numberOfPages;
    _indexs = @[].mutableCopy;
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.dotView.layer.cornerRadius = _selectSize.height/2.0;
    self.dotView.backgroundColor = _selectColor;
    for(int i=0; i<_numberOfPages; i++){
        UIView *pView = [UIView new];
        [self.bgView addSubview:pView];
        pView.frame = (CGRect){{0, 0}, _normalSize};
        pView.backgroundColor = _normalColor;
        pView.layer.cornerRadius = _normalSize.height/2.0;
        pView.tag = i+10;
        [self.indexs addObject:@(pView.tag)];
        if(_currentPage == i){
            pView.backgroundColor = _selectColor;
            pView.frame = (CGRect){{0, 0}, _selectSize};
            self.dotView = pView;
        }
    }
    [self setNeedsLayout];
}

- (void)setCurrentPage:(NSInteger)currentPage
{
    if(self.indexs.count){
        [self.indexs exchangeObjectAtIndex:_currentPage%_numberOfPages withObjectAtIndex:currentPage%_numberOfPages];
        [UIView animateWithDuration:self.pageAnimation?.25:0 animations:^{
            [self enumerateSubviews];
        }];
    }else{
        [self setNeedsLayout];
    }
    _currentPage = currentPage%_numberOfPages;
}

- (void)setNormalColor:(UIColor *)normalColor
{
    _normalColor = normalColor;
    [self.bgView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.backgroundColor = self.normalColor;
    }];
}

- (void)setSelectColor:(UIColor *)selectColor
{
    _selectColor = selectColor;
    self.dotView.backgroundColor = _selectColor;
}

- (void)setNormalSize:(CGSize)normalSize
{
    _normalSize = normalSize;
    [self.bgView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGRect rect = obj.frame;
        rect.size = self->_normalSize;
        obj.frame = rect;
        obj.layer.cornerRadius = self->_normalSize.height/2.0;
    }];
    [self setNeedsLayout];
}

- (void)setSelectSize:(CGSize)selectSize
{
    _selectSize = selectSize;
    self.dotView.layer.cornerRadius = _selectSize.height/2.0;
    CGRect rect = self.dotView.frame;
    rect.size = _selectSize;
    self.dotView.frame = rect;
    [self setNeedsLayout];
}

- (void)setSingleHide:(BOOL)singleHide
{
    _singleHide = singleHide;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self enumerateSubviews];
}

- (void)enumerateSubviews
{
    if(self.singleHide && self.numberOfPages==1){
        self.dotView.hidden = YES;
        return;
    }else{
        self.dotView.hidden = NO;
    }
    __block CGFloat f_x = (self.bounds.size.width-self.normalSize.width*self.numberOfPages-self.gap*(self.numberOfPages-1)+(self.selectSize.width-self.normalSize.width))/2.0 + self.offset;
    CGFloat f_y = (self.bounds.size.height-self.normalSize.height)/2.0;
    [self.indexs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *pView = [self.bgView viewWithTag:[obj integerValue]];
        CGRect rect = pView.frame;
        rect.origin = (CGPoint){f_x, f_y};
        pView.frame = rect;
        f_x += (CGRectGetWidth(rect)+self.gap);
    }];
}

#pragma mark - Getter
- (UIView *)bgView
{
    if(!_bgView){
        _bgView = [UIView new];
        [self addSubview:_bgView];
    }
    return _bgView;
}

@end
